package com.llkj.contractmanagement.contractmanagement.actor;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.adapter.ActorPhotoAdapter;
import com.llkj.contractmanagement.customview.RoundImageView;
import com.llkj.contractmanagement.customview.photoview.ViewPagerActivity;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.ActorBean;
import com.llkj.contractmanagement.model.SCZJBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.model.YrPicBean;
import com.llkj.contractmanagement.utils.GsonUtil;
import com.llkj.contractmanagement.utils.ImageLoaderUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/20 14:47
 * description: 艺人详情界面
 */
public class ActorDetailsActivity extends BaseActivity implements View.OnClickListener, ActorPhotoAdapter.OnItemClickLitener {



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_actordetails;
    }

    @Bind(R.id.rlv_pic)
    RecyclerView mRecyclerView;
    @Bind(R.id.rl_actor_info)
    RelativeLayout rlInfo;//基本信息
    @Bind(R.id.iv_actor_info)
    ImageView iv_actor_info;
    @Bind(R.id.rl_actor_main)
    RelativeLayout rlMain;//主要作品
    @Bind(R.id.iv_actor_main)
    ImageView iv_actor_main;
    @Bind(R.id.rl_actor_expence)
    RelativeLayout rlExpence;//演艺经历
    @Bind(R.id.iv_actor_expence)
    ImageView iv_actor_expence;
    @Bind(R.id.riv_actor_details_headphoto)
    RoundImageView riv_actor_head;//艺人头像

//    ActorInfoFragment actorInfoFragment;
//    ActorMainFragment actorMainFragment;
//    ActorExpenceFragment actorExpenceFragment;

    @Bind(R.id.iv_back_left)
    ImageView ivBack;
    @Bind(R.id.iv_right_collection)
    ImageView ivCollect;
    private ActorBean.YrlistBean yrlistBean;
    private ArrayList<String> picUrlList;
    private ActorPhotoAdapter picAdapter;
    private UserInfoBean userInfoBean;
    private boolean isCollect = false;
    @Bind(R.id.webview_actor_details)
    WebView webView;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();

    }

    private void init() {
        initDatas();
        setListener();
        initWebview();
    }

    private void initWebview() {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(false);// 设定支持缩放
        webView.loadUrl(UrlConfig.YR_DETAIL + "&yr_id=" + yrlistBean.getYr_id());
        setSelected(rlInfo);
    }

    private void setListener() {
        ivCollect.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        rlExpence.setOnClickListener(this);
        rlInfo.setOnClickListener(this);
        rlMain.setOnClickListener(this);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
    }

    private void initDatas() {
        userInfoBean = UserInfoBean.getInstance();
        Intent intent = getIntent();
        if (intent != null) {
            yrlistBean = (ActorBean.YrlistBean) getIntent().getSerializableExtra("YrList");
            if (yrlistBean != null) {
                ImageLoaderUtil.displayHeaderImage(yrlistBean.getLogo(), riv_actor_head);
                if (!TextUtils.isEmpty(yrlistBean.getSc())) {
                    if (yrlistBean.getSc().equals("1")) {
                        ivCollect.setImageResource(R.drawable.icon_del_collect);
                    } else if (yrlistBean.getSc().equals("0")) {
                        ivCollect.setImageResource(R.drawable.icon_collect);
                    }
                }
            }
        }
        picUrlList = new ArrayList<>();
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //设置适配器
        picAdapter = new ActorPhotoAdapter(this, picUrlList);
        mRecyclerView.setAdapter(picAdapter);
        picAdapter.setOnItemClickLitener(this);
//        setSelection(0);
        requestYrPic();//请求艺人图库接口
        requestAddYrZj();//请求艺人足迹添加接口
    }

    private void requestAddYrZj() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户ID
//        yr_id	1	string	艺人id
        map.put("user_id", userInfoBean.getId());
        map.put("yr_id", yrlistBean.getYr_id());
        HttpMethod.yrZj(this, this, map, UrlConfig.HTTP_YR_ZJ);
    }

    private void requestYrPic() {
        //        yr_id	1	string	艺人id
        HashMap<String, String> map = new HashMap<>();
        map.put("yr_id", yrlistBean.getYr_id());
        HttpMethod.yrPic(this, this, map, UrlConfig.HTTP_YR_PIC);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_actor_info://基本信息
                setSelected(rlInfo);
                webView.loadUrl(UrlConfig.YR_DETAIL + "&yr_id=" + yrlistBean.getYr_id());
                break;
            case R.id.rl_actor_main://主要作品
                setSelected(rlMain);
                webView.loadUrl(UrlConfig.YR_DETAIL1 + "&yr_id=" + yrlistBean.getYr_id());
                break;
            case R.id.rl_actor_expence://演艺经历
                setSelected(rlExpence);
                webView.loadUrl(UrlConfig.YR_DETAIL2 + "&yr_id=" + yrlistBean.getYr_id());
                break;
            case R.id.iv_back_left:
                this.finish();
                break;
            case R.id.iv_right_collection://收藏
                if (yrlistBean.getSc().equals("1")) {
                    //1-已收藏状态-点击取消收藏
                    delCollect();
                }else if (yrlistBean.getSc().equals("0")) {
                    //0-未收藏状态-点击收藏
                    collect();
                }
                break;
        }
    }

    /*
    * 取消收藏艺人
    * */
    private void delCollect() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户ID
//        yr_id	1	string	艺人id
        map.put("user_id", userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        map.put("sc_id", yrlistBean.getSc_id());
        HttpMethod.delCollection(this, this, map, UrlConfig.HTTP_DELCOLLECTION);
    }

    /*
    * 收藏艺人
    * */
    private void collect() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户ID
//        yr_id	1	string	艺人id
        map.put("user_id", userInfoBean.getId());
        map.put("yr_id", yrlistBean.getYr_id());
        HttpMethod.scZj(this, this, map, UrlConfig.HTTP_SC_ZJ);
    }


    private void setSelected(View view) {
        rlInfo.setSelected(false);
        rlMain.setSelected(false);
        rlExpence.setSelected(false);
        view.setSelected(true);
        if (view.getId() == R.id.rl_actor_info) {
            iv_actor_info.setVisibility(view.VISIBLE);
            iv_actor_main.setVisibility(view.INVISIBLE);
            iv_actor_expence.setVisibility(view.INVISIBLE);
        } else if (view.getId() == R.id.rl_actor_main) {
            iv_actor_main.setVisibility(view.VISIBLE);
            iv_actor_info.setVisibility(view.INVISIBLE);
            iv_actor_expence.setVisibility(view.INVISIBLE);
        } else if (view.getId() == R.id.rl_actor_expence) {
            iv_actor_expence.setVisibility(view.VISIBLE);
            iv_actor_info.setVisibility(view.INVISIBLE);
            iv_actor_main.setVisibility(view.INVISIBLE);
        }
    }


    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(this, ViewPagerActivity.class);
        intent.putExtra("image_list", picUrlList);
        intent.putExtra("position", position);
        startActivity(intent);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_YR_PIC://艺人图库
                YrPicBean yrPicBean = GsonUtil.GsonToBean(responseInfo, YrPicBean.class);
                if (yrPicBean != null) {
                    if (yrPicBean.getState() == 1) {
                        picUrlList.addAll(yrPicBean.getPiclist());
                        picAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case UrlConfig.HTTP_YR_ZJ://艺人足迹添加

                break;
            case UrlConfig.HTTP_SC_ZJ://收藏
                SCZJBean sczjBean = GsonUtil.GsonToBean(responseInfo, SCZJBean.class);
                if (sczjBean != null) {
                    ToastUtil.makeShortText(this, "收藏成功");
                    yrlistBean.setSc("1");
                    ivCollect.setImageResource(R.drawable.icon_del_collect);
                    yrlistBean.setSc_id(sczjBean.getSc_id());
                }
                break;
            case UrlConfig.HTTP_DELCOLLECTION:
                ToastUtil.makeShortText(this, "取消收藏成功");
                yrlistBean.setSc("0");
                ivCollect.setImageResource(R.drawable.icon_collect);
                break;
        }
    }
}
